<?php

// TODO: review

if (preg_match('/^[http|https]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[#~A-Za-z0-9\.\/%&=\?\-_:\+]+$/i',$_GET['url'])) {

  $urlTarget = urlencode(htmlentities($_GET['url']));
  $url1 = 'TODO/validation/html/?url=' . $urlTarget . '&raw=yes';
  $url2 = 'TODO/validation/css/?url=' . $urlTarget . '&raw=yes';
  $raw = htmlentities($_GET['raw']);

  // Revoke PHP readability
  $checkUrl = htmlentities($_GET['url']);
  $checkHandle = curl_init($checkUrl);
  curl_setopt($checkHandle,CURLOPT_RETURNTRANSFER,TRUE);
  $checkResponse = curl_exec($checkHandle);
  $checkResponseCode = curl_getinfo($checkHandle,CURLINFO_HTTP_CODE);
  // echo $checkResponseCode;
  if ($checkResponseCode == '404' || $checkResponseCode == '500') {
    echo '?';
    die;
  }

  $buffer1 = file_get_contents($url1);
  $buffer2 = file_get_contents($url2);

  // echo 'Debug 1: ' . $buffer1 . ', ' . $buffer2 . ' / ';

  $errors = ($buffer1 + $buffer2);

  // echo 'Debug 2: ' . $errors . ' / ';

  $errorsAdjusted = ($buffer1 + ceil($buffer2/10));

  // echo 'Debug 3: ' . $errorsAdjusted . ' / ';

  if ($raw != 'yes') {
    if ($errorsAdjusted > 9) {
      echo '0';
    } else {
      $errorsTemp = (10 - $errorsAdjusted);
      echo $errorsTemp;
    }
  } else {
    echo $errors;
  }

}

?>